package com.lzy.liujing.restaurant.controller.sysController;

import com.lzy.liujing.restaurant.Enums.ResultEnum;
import com.lzy.liujing.restaurant.entity.*;
import com.lzy.liujing.restaurant.service.MemberCategoryService;
import com.lzy.liujing.restaurant.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/membercategory")
public class MemberCategoryController {
    @Autowired
    private MemberCategoryService memberCategoryService;

    @GetMapping("/current/{current}/size/{size}")
    public Result getAllPage(@PathVariable Integer current, @PathVariable Integer size) {
        PageItem<MemberCategory> brandPageItem = memberCategoryService.queryPage(current, size);

        Result result = new Result(brandPageItem, "查询成功");
        return result;

    }


    @PostMapping("/current/{current}/size/{size}")
    public Result getCondPage(@PathVariable Integer current,
                              @PathVariable Integer size,
                              @RequestBody MemberCategory query) {
        //执行查询
        PageItem<MemberCategory> item = memberCategoryService.queryConditionPage(query,current, size);

        Result result = new Result(item, "查询成功");
        return result;

    }

    @DeleteMapping("/membercates/{id}")
    public Result delete(@PathVariable Integer id, HttpServletRequest request) {
        Integer accountId = (Integer) request.getAttribute("accountId");
        boolean flag = memberCategoryService.delete(id);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }

    @GetMapping("/membercate/{id}")
    public Result getOne(@PathVariable Integer id) {
        MemberCategory byId = memberCategoryService.getById(id);
        if (byId == null) {
            return Result.fail(404, "查询不到该数据");
        }
        Result result = new Result(byId, "查询成功");
        return result;
    }

    @DeleteMapping("/membercate/{ids}")
    public Result deleteMultiRec(@PathVariable int[] ids) {
        boolean flag = memberCategoryService.deleteSome(ids);
        if (flag) {
            return new Result(null, "删除成功 ！");
        } else {
            return Result.fail(500, "删除失败 ！");
        }

    }
    @PostMapping("/membercate")
    // ResponseBody 将json字符串 转换为java 列表
    public Result insertRec(@RequestBody MemberCategory brand) {

        System.out.println(brand);
        boolean insertBrand = memberCategoryService.insert(brand);
        if (insertBrand) {
            return new Result(brand, "添加成功");
        } else {
            return Result.fail(500, "添加失败");
        }
    }

    @RequestMapping("/membercate/update")
    // ResponseBody 将json字符串 转换为java 列表
    public Result updateRed(@RequestBody MemberCategory brand) {

        boolean updateBrand = memberCategoryService.update(brand);
        if(updateBrand){
            return new Result(brand, "修改成功");
        } else {
            return Result.fail(500, "修改失败");
        }
    }
}
